package com.fjwt.gz.zhyx.sms;

import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.cache.RedisUtil;
import com.fjwt.gz.core.constants.CS;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.zhyx.ZhyxKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

@Slf4j
public class SmsUtil {
    /**组装参数**/
    public static JSONObject assembleSmsCode(String sysType, String phone, String verifyCode, Integer flag) {
        String content = "";
        // 将短信验证码存在redis中
        JSONObject codeObj = new JSONObject();
        codeObj.put("phone", phone);
        codeObj.put("code", verifyCode);
        codeObj.put("smsType", ZhyxKit.SMS_TYPE);

        log.info("手机号:{},短信验证码:{}", phone, verifyCode);
        content = String.format(ZhyxKit.SMS_CONTENT,verifyCode);
        codeObj.put("content", content);
        // 短信验证码缓存时间: 5分钟
        // flag: 0:登录 1:修改密码 2:添加用户 3:商户认证 4:未知 5:迁移积分 6:预约有礼 7:虚拟券充值 8:商户认证自主发券 9:小程序用户注销 10:用户实名认证
        if(0 == flag){
            RedisUtil.setString(CS.getLoginCacheKeySmsCode(sysType, phone), codeObj.toJSONString(), 60*5);
        }else if(1 == flag){
            RedisUtil.setString(CS.getUpdatepwdCacheKeySmsCode(sysType, phone), codeObj.toJSONString(), 60*5);
        }else if(2 == flag){
            RedisUtil.setString(CS.addUserCacheKeySmsCode(sysType, phone), codeObj.toJSONString(), 60*5);
        }else if(3== flag){
            RedisUtil.setString(CS.getCacheMchAuthSmsCode(sysType, phone), codeObj.toJSONString(), 60*5);
        }else if(4 == flag){
            RedisUtil.setString(CS.getCacheKeySmsCode(sysType, phone), codeObj.toJSONString(), 60*5);
        }else if(5 == flag){
            RedisUtil.setString(CS.getMoveScoreCacheKeySmsCode(sysType, phone), codeObj.toJSONString(), 60*5);
        }else if(6 == flag){
            RedisUtil.setString(CS.getBookGiftSendMsg(sysType, phone),codeObj.toJSONString(),60*5);
        }else if(7 == flag){
            RedisUtil.setString(CS.getVirtualAwardTopUpSmsCode(sysType, phone),codeObj.toJSONString(),60*5);
        }else if(8 == flag){
            RedisUtil.setString(CS.getCouponMchUserCode(sysType, phone),codeObj.toJSONString(),60*5);
        }else if(9 == flag){
            RedisUtil.setString(CS.getCacheKeyFansUserLogout(sysType, phone),codeObj.toJSONString(),60*5);
        }else if(10 == flag){
            RedisUtil.setString(CS.getCacheKeyFansUserRealName(sysType, phone),codeObj.toJSONString(),60*5);
        }
        codeObj.remove("code");
        return  codeObj;
    }
    public static JSONObject getSmsCOde(String sysType, String phone,Integer flag){
        String codeObj = "";
        if(0 == flag){
            codeObj = RedisUtil.getString(CS.getLoginCacheKeySmsCode(sysType, phone));
        }else if(1 == flag){
            codeObj = RedisUtil.getString(CS.getUpdatepwdCacheKeySmsCode(sysType, phone));
        }else if(2 == flag){
            codeObj = RedisUtil.getString(CS.addUserCacheKeySmsCode(sysType, phone));
        }else if(3== flag){
            codeObj = RedisUtil.getString(CS.getCacheMchAuthSmsCode(sysType, phone));
        }else if(4 == flag){
            codeObj = RedisUtil.getString(CS.getCacheKeySmsCode(sysType, phone));
        }else if(5 == flag){
            codeObj = RedisUtil.getString(CS.getMoveScoreCacheKeySmsCode(sysType, phone));
        }else if(6 == flag){
            codeObj = RedisUtil.getString(CS.getBookGiftSendMsg(sysType, phone));
        }else if(7 == flag){
            codeObj = RedisUtil.getString(CS.getVirtualAwardTopUpSmsCode(sysType, phone));
        }else if(8 == flag){
            codeObj = RedisUtil.getString(CS.getCouponMchUserCode(sysType, phone));
        }else if(9 == flag){
            codeObj = RedisUtil.getString(CS.getCacheKeyFansUserLogout(sysType, phone));
        }else if(10 == flag){
            codeObj = RedisUtil.getString(CS.getCacheKeyFansUserRealName(sysType, phone));
        }
        if (StringUtils.isEmpty(codeObj)){
            throw new BizException("验证码错误");
        }
        JSONObject smsJodeJson = JSONObject.parseObject(codeObj);
        return smsJodeJson;
    }
}
